package com.ivr.cm.handler.validate.impl;

import java.util.AbstractMap;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import com.ivr.cm.cache.Cacheable;
import com.ivr.cm.data.ValidateData;
import com.ivr.cm.handler.validate.ValidateServiceIF;
import com.ivr.cm.service.StateCode;
import com.ivr.cm.vo.ConfigBusinessVo;

/**
 * 省份,城市鉴权
 * @author liugeng
 * @date 2015-12-15
 *
 */
public class ValidateForbiddenAreaImpl implements ValidateServiceIF {
	private static Logger logger = Logger.getLogger(ValidateForbiddenAreaImpl.class);
	private Cacheable configBusinessCache;

	@Override
	public int doProcess(ValidateData validateData) {
		try{
			/**获得主叫省份*/
			String province = validateData.getBaseData().getProvince().trim();
			/**获得主叫城市*/
			String city = validateData.getBaseData().getCity().trim();
			/**获得主叫号码*/
			String mobile = validateData.getBaseData().getCalling();
			/**获得业务号码短号码*/
			String sp_code = validateData.getBaseData().getShort_called();
			logger.info(String.format("[%s][%s][省市屏蔽鉴权]开始", mobile, sp_code));
			
			/**未知省份是否进行鉴权过滤，现在是未知省份进行鉴权失败操作*/
	//		if("未知".equals(province) || "未知".equals(city)){
	//			logger.info(String.format("[%s][%s][省市屏蔽鉴权]未通过,[%s][%s]未知省市默认被屏蔽", mobile, sp_code, province, city));
	//			validateData.setResult(ValidateData.RESULT_INVALID_OTHER);
	//			validateData.setVoice(ValidateData.VOICE_INVALID_OTHER);
	//			validateData.setTimeRemain(0);
	//			validateData.setStatecode(StateCode.PROVINCE_LIMIT);
	//			return ValidateChain.VALIDATE_FINISHED;
	//		}
			
			/**从缓存中获得SP号码的业务配置信息*/
			AbstractMap<String, ConfigBusinessVo> configBusinessMap = (AbstractMap<String, ConfigBusinessVo>) configBusinessCache.getCache();
			ConfigBusinessVo configBusinessVo = configBusinessMap.get(sp_code);
			
			/**判断主叫号码是否在屏蔽省份内*/
			if(configBusinessVo.getForbidden_province().indexOf(province) >= 0){
				logger.info(String.format("[%s][%s][省市屏蔽鉴权]未通过,[%s]被屏蔽,业务屏蔽省份[%s]",mobile, sp_code, province,configBusinessVo.getForbidden_province()));
				validateData.setResult(ValidateData.RESULT_INVALID_OTHER);
				validateData.setVoice(ValidateData.VOICE_INVALID_OTHER);
				validateData.setTimeRemain(0);
				validateData.setStatecode(StateCode.PROVINCE_LIMIT);
				return ValidateChain.VALIDATE_FINISHED;
			}
			
			/**判断主叫号码是否在屏蔽城市内*/
			if(configBusinessVo.getForbidden_city().indexOf(city) >= 0){
				logger.info(String.format("[%s][%s][省市屏蔽鉴权]未通过,[%s]被屏蔽,业务屏蔽城市[%s]",mobile, sp_code, city,configBusinessVo.getForbidden_city()));
				validateData.setResult(ValidateData.RESULT_INVALID_OTHER);
				validateData.setVoice(ValidateData.VOICE_INVALID_OTHER);
				validateData.setTimeRemain(0);
				validateData.setStatecode(StateCode.CITY_LIMIT);
				return ValidateChain.VALIDATE_FINISHED;
			}
			
			logger.info(String.format("[%s][%s][省市屏蔽鉴权]通过,[%s][%s]未被屏蔽,业务屏蔽省[%s]市[%s]", mobile, sp_code, province, city, configBusinessVo.getForbidden_province(), configBusinessVo.getForbidden_city()));
			return ValidateChain.VALIDATE_CONTINUE;
		}catch(Exception e){
			logger.info(String.format("[省市屏蔽鉴权]异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		return ValidateChain.VALIDATE_CONTINUE;
	}

	public Cacheable getConfigBusinessCache() {
		return configBusinessCache;
	}
	public void setConfigBusinessCache(Cacheable configBusinessCache) {
		this.configBusinessCache = configBusinessCache;
	}
}